package com.arvinclub.web.controller.blog;

import com.arvinclub.model.entity.Blog;
import com.arvinclub.model.entity.User;
import com.arvinclub.model.util.ToolsUtil;
import com.arvinclub.service.service.BlogService;
import com.arvinclub.service.service.UserService;
import com.github.pagehelper.PageInfo;
import org.hibernate.validator.internal.util.privilegedactions.GetResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 博客相关
 */
@Controller
public class BlogsController {

    @Resource
    private BlogService blogService;
    @Resource
    private UserService userService;
    private final String CLASSPATH = GetResource.class.getClassLoader().getResource("/").getPath();

    /**
     * 查看博客（默认第一页）
     */
    @GetMapping("user/blogs.html")
    public String showBlogs(Model model) {
        return showBlogs(model, 1);
    }

    /**
     * 查看博客（指定页号）
     *
     * @param page 页号
     */
    @GetMapping("user/blogs.html/{page}")
    public String showBlogs(Model model, @PathVariable int page) {
        /*根据页号获取数据*/
        PageInfo<Blog> blogPage = blogService.selectAllBlog(page);
        model.addAttribute("blogPage", blogPage);
        return "blogs";
    }

    /**
     * 查看关注动态（默认第一页）
     */
    @GetMapping("user/attention.html")
    public String attention(Model model,HttpSession session) {
        return attention(model, 1, session);
    }

    /**
     * 查看关注动态（指定页号）
     *
     * @param page 页号
     */
    @GetMapping("user/attention.html/{page}")
    public String attention(Model model, @PathVariable int page,HttpSession session) {
        /*根据页号获取数据*/
        User me = (User) session.getAttribute("user");
        PageInfo<Blog> blogPage = blogService.attentionBlogs(me.getId(), page);
        model.addAttribute("blogPage", blogPage);
        return "attention";
    }

    /**
     * 发布博客
     *
     * @param blog  博客
     * @param files 图片文件
     * @return 成功则转到社区主页，否则转到错误提示页面
     */
    @PostMapping("user/addBlog")
    public String addBlog(Blog blog, HttpSession httpSession, @RequestParam MultipartFile[] files) throws Exception {
        User me = (User) httpSession.getAttribute("user");
        userService.checkMuted(me);
        /*给blog添加filenames（面向数据库的图片文件名）*/
        blog.setFilenames(ToolsUtil.saveIMG(files, CLASSPATH));
        blog.setUser(me);
        /*添加博客*/
        blogService.addBlog(blog);
        return "redirect:/user/blogs.html";
    }

    /**
     * 删除博客
     *
     * @param blogId blog_id
     * @return 成功则转到个人主页，否则转到错误提示页面
     */
    @GetMapping("user/delBlog/{blogId}")
    public String delBlog(@PathVariable int blogId, HttpSession httpSession) {
        /*如果有编辑权限则删除*/
        Blog blog = blogService.blogDetail(blogId);
        User me = (User) httpSession.getAttribute("user");
        User someone = blog.getUser();
        if (me.equals(someone) || me.getAdmin() > someone.getAdmin())
            blogService.delBlog(blogId);
        return "redirect:/user/page.html/" + blog.getUser().getId();
    }

    /**
     * 编辑博客
     *
     * @return 成功则转到个人主页，否则转到错误提示页面
     */
    @PostMapping("user/editBlog")
    public String editBlog(Blog blog, HttpSession httpSession) {
        /*如果有编辑权限则删除*/
        User me = (User) httpSession.getAttribute("user");
        userService.checkMuted(me);
        Blog oldBlog = blogService.blogDetail(blog.getId());
        User someone = oldBlog.getUser();
        if (me.equals(someone) || me.getAdmin() > someone.getAdmin()) {
            blog.setContent(blog.getContent().replaceAll("\\\\r\\\\n","\r\n"));
            blogService.editBlog(blog);
        }
        return "redirect:/user/detail.html/" + blog.getId();
    }

    /**
     * 查看详情
     *
     * @param blogId blog_id
     * @return 成功则转到博客详情，否则转到错误提示页面
     */
    @GetMapping("user/detail.html/{blogId}")
    public String blogDetail(Model model, @PathVariable int blogId, HttpSession httpSession) {
        /*获取blog*/
        Blog blog = blogService.blogDetail(blogId);
        /*是否有编辑权限*/
        User me = (User) httpSession.getAttribute("user");
        User someone = blog.getUser();
        model.addAttribute("blog", blog);
        String content = blog.getContent().replaceAll("\\r\\n", "\\\\r\\\\n");
        System.out.println(content);
        model.addAttribute("content", content);
        model.addAttribute("host", me.equals(someone) || me.getAdmin() > someone.getAdmin());
        return "detail";
    }
}
